package android.slc.appbase.ui.vd;

import android.os.Bundle;
import android.slc.appbase.utils.AppIntentUtils;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.contract.ActivityResultContract;

import java.io.Serializable;

/**
 * @author slc
 * @date 2024/1/15
 **/
public abstract class BaseRegisterActivityFunc<T extends Serializable> {

    private IntentTargetWrapper intentTargetWrapper;

    private OnActivityResultCallback<T> onActivityResultCallback;

    public BaseRegisterActivityFunc(IntentTargetWrapper intentTargetWrapper) {
        this(intentTargetWrapper, null);
    }

    public BaseRegisterActivityFunc(IntentTargetWrapper intentTargetWrapper, OnActivityResultCallback<T> OnActivityResultCallback) {
        this.intentTargetWrapper = intentTargetWrapper;
        this.onActivityResultCallback = OnActivityResultCallback;
    }

    public IntentTargetWrapper getIntentTargetWrapper() {
        return intentTargetWrapper;
    }

    public void setOnActivityResultCallback(OnActivityResultCallback<T> OnActivityResultCallback) {
        this.onActivityResultCallback = OnActivityResultCallback;
    }

    public OnActivityResultCallback<T> getOnTargetActivityResultCallback() {
        return onActivityResultCallback;
    }

    public abstract ActivityResultContract<Bundle, Bundle> getTargetActivityResultContract();

    public interface OnActivityResultCallback<T extends Serializable> extends ActivityResultCallback<Bundle> {

        default void onActivityResult(Bundle result) {
            if (result != null || notifyNull()) {
                T data = AppIntentUtils.getDataByBundle(result);
                try {
                    onIntentData(data);
                } catch (ClassCastException e) {
                }
                onIntentBundle(result);
            }
        }

        void onIntentData(T data);

        default void onIntentBundle(Bundle data) {
        }

        default boolean notifyNull() {
            return false;
        }
    }

    public interface IIntegrationRegisterActivityFunc {

        BaseRegisterActivityFunc<Serializable> createActionRegisterActivityFunc();

        IntentTargetWrapper getGotoTargetActivityItw();
    }

    public static IntentTargetWrapper createItByTargetClass(Class<?> targetClass) {
        IntentTargetWrapper intentTargetWrapper = new IntentTargetWrapper();
        intentTargetWrapper.setTargetClass(targetClass);
        return intentTargetWrapper;
    }

    public static IntentTargetWrapper createItByIntentAction(String intentAction) {
        IntentTargetWrapper intentTargetWrapper = new IntentTargetWrapper();
        intentTargetWrapper.setIntentAction(intentAction);
        return intentTargetWrapper;
    }

    public static class IntentTargetWrapper {
        private Class<?> targetClass;
        private String intentAction;

        public Class<?> getTargetClass() {
            return targetClass;
        }

        public void setTargetClass(Class<?> targetClass) {
            this.targetClass = targetClass;
        }

        public String getIntentAction() {
            return intentAction;
        }

        public void setIntentAction(String intentAction) {
            this.intentAction = intentAction;
        }

        public boolean isTargetClassMode() {
            return targetClass != null;
        }

        public boolean isIntentActionMode() {
            return intentAction != null && !intentAction.isEmpty();
        }
    }
}
